<?php
/**
 * 权限菜单Model
 * UserModel：LLw
 * Date: 2020-01-07
 * Time：14：43
 */
namespace app\caidongweb\model;

/**
 * Class Rule
 * 权限菜单逻辑模块
 * @package app\caidongweb\model
 */
class Rule extends BaseModel
{
    protected $table = 'cd_auth_rule';

    //权限菜单
    public function ruleList(){
        $where = [];
        input('key') ? $where['title'] = input('key') : false;
        $list = $this->where('pid',0)->where($where)->select();
        foreach ($list as $key=>$val){
            $list[$key]['class'] = $this->where('pid',$val['id'])->select();
        }
        return $list;
    }

    /**
     * function getList
     * 获取导航栏列表
     * @return object
     */
    public function getList()
    {
        $list = $this->where('pid',0)->select();
        foreach($list as $key=>$val){
            $child_list = $this->where('pid',$val['id'])->select();
            $list[$key]['child'] = $child_list;
        }
        return $list;
    }

    /**
     * 添加修改
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getEdit()
    {
        $data = input('post.');
        if($data){
            $date['name'] = $data['name'];
            $date['title'] = $data['title'];
            $date['pid'] = $data['pid'];
            $date['condition'] = $data['condition'];
            if($data['id']){
                $res = $this->where('id',$data['id'])->update($data);
            }else{
                $res = $this->insert($data);
            }
            if($res){
                return $this->returnData(1,'成功');
            }else{
                return $this->returnData(-1,'失败');
            }
        }else{
            $id = input('id');
            $ruleList = $this->where('id',$id)->find();
            return $ruleList;
        }
    }

    /**
     * 删除
     * @return int
     */
    public function delList()
    {
        $id = input('post.id');
        $res = $this->where('id',$id)->delete();
        return $res;
    }
}